package user;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JViewport;

import control.IndexFields;


public class Janela extends JFrame implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JEditorPane results;
	private JTextField inputQuery;
	private JComboBox fieldQuery;
	private JScrollPane scrollPane;
	private JButton sendButton;
	private boolean closingCommand;
	private boolean newMsg;
	

	public Janela(String title) throws HeadlessException {
		super(title);
		// TODO Auto-generated constructor stub
	}

	public void mostraJanela() {
		setSize(new Dimension(800,500));
		setMinimumSize(new Dimension(100, 50));
		setBounds(400, 200, 800, 500);
		setResizable(false);
		setLocation(100, 200);
		closingCommand = false;
		newMsg = false;
		
		inputQuery = new JTextField(50);
		inputQuery.setSize(new Dimension(500, 10));
		inputQuery.setFont(new Font("Arial", Font.PLAIN, 12));
		inputQuery.setPreferredSize(new Dimension(400, 15));
		inputQuery.addActionListener(this);
		
		results = new JEditorPane();
//		results.setColumns(30);
//		results.setRows(50);
		results.setContentType("text/html");
		results.setPreferredSize(new Dimension(600, 500));
		results.setEditable(false);
//		results.setWrapStyleWord(true);
		results.setAutoscrolls(true);
//		results.setLineWrap(true);
//		results.setText("<a href='#' style='font-family: sans-serif;font-size:15px;text-decoration: none;'>Teste</a>");
		
		List<String> fields = IndexFields.getValues();
		fields.add(0, "");
		fields.remove(fields.size() - 1);
		fields.remove(fields.size() - 1);
		fieldQuery = new JComboBox(fields.toArray());
		fieldQuery.addActionListener(this);

		scrollPane = new JScrollPane(results);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setPreferredSize(new Dimension(600, 200));
		
		//String[] users = onlineUsers.split(",");
		
		

		
		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		
		sendButton = new JButton("Enviar");
		sendButton.addActionListener(this);
		
		layout.setVerticalGroup(layout.createSequentialGroup()
				.addGroup(layout.createParallelGroup(Alignment.LEADING)
					.addComponent(inputQuery)
					.addComponent(fieldQuery)
					.addComponent(sendButton))
				.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane)));

		layout.setHorizontalGroup(layout.createParallelGroup()
				.addGroup(layout.createParallelGroup(Alignment.LEADING))
				.addGroup(layout.createSequentialGroup()
						.addComponent(scrollPane))
						.addGroup(layout.createSequentialGroup()
								.addComponent(inputQuery)
								.addComponent(fieldQuery)
								.addComponent(sendButton)));

		
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
			     onExit();
			   }

		});
		setVisible(true);
	}

	

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
//			Object[] selected = onlineUsersList.getSelectedValues();
//			String recipients = new String("");
//			for(Object obj : selected){
//				recipients+= "@"+(String)obj+" ";
//			}
//			onlineUsersList.clearSelection();
//			setMessageFromUser(recipients+inputMessage.getText());
//			setNewMsg(true);
//			inputMessage.setText("");
//		if(e.getSource() instanceof JTextField || e.getSource() instanceof JButton){
//			JComboBox combo = (JComboBox) e.getSource();
//			System.out.println(combo.getSelectedItem().toString());
//		}
//		System.out.println(fieldQuery.getSelectedItem().toString());
		if(e.getSource() instanceof JComboBox == false){
			results.setText(Searcher.searchString(inputQuery.getText(), IndexFields.fromString(fieldQuery.getSelectedItem().toString())));
			results.setCaretPosition(0);
			JViewport viewPort = scrollPane.getViewport();
			viewPort.setViewPosition(new Point(0, 0));
		}
		
	}



	
	public void setClosingCommand(boolean closingCommand) {
		this.closingCommand = closingCommand;
	}

	public boolean getClosingCommand() {
		return closingCommand;
	}

	public void onExit(){
		closingCommand=true;
	}

	
	

	public void setNewMsg(boolean newMsg) {
		this.newMsg = newMsg;
	}

	public boolean getNewMsg() {
		return newMsg;
	}
	
}
